import 'dart:math';

import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGButton.dart';

class QGBatchAlert extends StatefulWidget {
  String cancel;
  String confirm;
  void Function()? onCancel;
  void Function(String price, String count, String payType)? onConfirm;
  BuildContext context;

  QGBatchAlert({
    super.key,
    required this.context,
    required this.cancel,
    required this.confirm,
    this.onCancel,
    this.onConfirm,
  });

  static show(context, {cancel = "取消", confirm = "确定", onCancel, onConfirm}) {
    showDialog(
        context: context,
        builder: (context) {
          return QGBatchAlert(
              context: context,
              cancel: cancel,
              confirm: confirm,
              onCancel: onCancel,
              onConfirm: onConfirm);
        });
  }

  @override
  State<QGBatchAlert> createState() => _QGBatchAlertState();
}

class _QGBatchAlertState extends State<QGBatchAlert> {
  var priceController = TextEditingController();
  var countController = TextEditingController();
  var payType = "WALLET_PAY";

  int count = 3;
  int minCount = 3;
  int maxCount = 99999;

  @override
  void dispose() {
    priceController.dispose();
    countController.dispose();

    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    priceController.text = "500";
    countController.text = "$count";

    countController.addListener(() {
      var ret = int.tryParse(countController.text);

      if (ret is int) {
        count = ret;

        if (count > maxCount) {
          countController.text = "$maxCount";
        } else if (count < minCount) {
          countController.text = "$minCount";
        }
        count = min(maxCount, count);
        count = max(minCount, count);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        GestureDetector(
          onTap: () {
            unfocus(context);
          },
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 20.rpx),
            padding: EdgeInsets.symmetric(horizontal: 20.rpx, vertical: 25.rpx),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20.rpx),
              color: Color(0xFF232A32),
            ),
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              buildHeader(),
              SizedBox(height: 30.rpx),
              buildButtons(),
            ]),
          ),
        ),
      ],
    );
  }

  buildHeader() {
    return Column(
      children: [
        Text(
          "批量下单",
          style: TextStyle(
              color: Color(0xFFFFFFFF),
              fontSize: 16.rpx,
              fontWeight: FontWeight.bold),
        ),
        SizedBox(height: 25.rpx),
        buildPrice(),
        buildCount(),
      ],
    );
  }

  buildPrice() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.rpx),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          "限价",
          style: TextStyle(
              color: Color(0xFF989EA4),
              fontSize: 14.rpx,
              fontWeight: FontWeight.bold),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              alignment: Alignment.center,
              width: 35.rpx,
              child: Text(
                "最高",
                style: TextStyle(
                    color: Color(0xFFFFFFFF),
                    fontSize: 14.rpx,
                    fontWeight: FontWeight.bold),
              ),
            ),
            SizedBox(width: 5.rpx),
            SizedBox(
              width: 80.rpx,
              child: TextField(
                textAlign: TextAlign.center,
                controller: priceController,
                keyboardType:
                    const TextInputType.numberWithOptions(decimal: true),
                textInputAction: TextInputAction.done,
                cursorColor: Color(0xFFFFFFFF),
                style: TextStyle(
                    color: Color(0xFFFFFFFF),
                    fontSize: 16.rpx,
                    fontWeight: FontWeight.bold),
                decoration: const InputDecoration(
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                ),
              ),
            ),
            SizedBox(width: 5.rpx),
            Container(
              width: 20.rpx,
              alignment: Alignment.center,
              child: Text(
                "元",
                style: TextStyle(
                    color: Color(0xFFFFFFFF),
                    fontSize: 14.rpx,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ],
        )
      ]),
    );
  }

  buildCount() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.rpx),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          "数量",
          style: TextStyle(
              color: Color(0xFF989EA4),
              fontSize: 14.rpx,
              fontWeight: FontWeight.bold),
        ),
        Row(
          children: [
            GestureDetector(
              onTap: onSub,
              child: Container(
                width: 35.rpx,
                alignment: Alignment.center,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20.rpx),
                        color: Color(0xFFFFFFFF),
                      ),
                      alignment: Alignment.center,
                      width: 20.rpx,
                      height: 20.rpx,
                      child: Text(
                        '-',
                        style: TextStyle(
                            color: Color(0xFF111111),
                            fontSize: 15.rpx,
                            fontWeight: FontWeight.normal),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(width: 5.rpx),
            SizedBox(
              width: 80.rpx,
              child: TextField(
                textAlign: TextAlign.center,
                controller: countController,
                keyboardType: TextInputType.number,
                textInputAction: TextInputAction.done,
                cursorColor: Color(0xFFFFFFFF),
                style: TextStyle(
                    color: Color(0xFFFFFFFF),
                    fontSize: 16.rpx,
                    fontWeight: FontWeight.bold),
                decoration: const InputDecoration(
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                ),
              ),
            ),
            SizedBox(width: 5.rpx),
            GestureDetector(
              onTap: onAdd,
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20.rpx),
                  color: Color(0xFFFFFFFF),
                ),
                alignment: Alignment.center,
                width: 20.rpx,
                height: 20.rpx,
                child: Text(
                  '+',
                  style: TextStyle(
                      color: Color(0xFF111111),
                      fontSize: 15.rpx,
                      fontWeight: FontWeight.normal),
                ),
              ),
            ),
          ],
        ),
      ]),
    );
  }

  buildButtons() {
    return SizedBox(
      height: 120.rpx,
      child: Column(
        children: [
           Expanded(
              child: QGButton(
                  title: widget.confirm, enable: true, onTap: onConfirm)),
          SizedBox(width: 15.rpx),
          Expanded(
              child: QGOutlineButton(
                  title: widget.cancel,
                  enable: true,
                  onTap: () {
                    if (widget.onCancel != null) {
                      widget.onCancel!();
                    } else {
                      Navigator.of(widget.context).pop();
                    }
                  })),
         
        ],
      ),
    );
  }

  onAdd() {
    unfocus(context);
    count++;
    count = min(maxCount, count);
    countController.text = "$count";
    if (mounted) setState(() {});
  }

  onSub() {
    unfocus(context);
    count--;
    count = max(minCount, count);
    countController.text = "$count";
    if (mounted) setState(() {});
  }

  onConfirm() {
    unfocus(context);
    if (widget.onConfirm != null) {
      var price = priceController.text;
      var count = countController.text;
      if (price.isEmpty || count.isEmpty) {
        toast("请检查限价和数量");
        return;
      }
      widget.onConfirm!(price, count, payType);
    } else {
      Navigator.of(widget.context).pop();
    }
  }
}
